package com.t216.dm216.service.impl;

import cn.hutool.core.lang.UUID;
import com.mysql.cj.util.TimeUtil;
import com.t216.dm216.common.Constant;
import com.t216.dm216.common.DateUtils;
import com.t216.dm216.mapper.DmOrderMapper;
import com.t216.dm216.pojo.*;
import com.t216.dm216.pojo.dto.DmOrderParamsDTO;
import com.t216.dm216.pojo.dto.DmSeatPriceOrderDTO;
import com.t216.dm216.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class DmOrderServiceImpl implements DmOrderService {
    @Resource
    private DmOrderMapper orderMapper;

    @Resource
    private DmItemService itemService;

    @Resource
    private DmSchedulerSeatPriceService schedulerSeatPriceService;

    @Resource
    private DmLinkUserService linkUserService;

    @Resource
    private DmOrderLinkUserService orderLinkUserService;

    @Resource
    private DmSchedulerSeatService schedulerSeatService;

    @Transactional
    @Override
    public DmOrder addOrder(DmOrderParamsDTO orderParamsDTO) {
        DmOrder order = new DmOrder();
        /*这句代码通常要写在第一句*/
        BeanUtils.copyProperties(orderParamsDTO, order);
        //生成订单号 拿到节目名称 绑定发票编号 获取保险价格 拿到商品总数 拿到商品总价
        //年月日时分秒+6位随机数  我们可以暂时就拿年月日时分秒毫秒
        String orderNo = DateUtils.getTimeMills();

        DmItem item = itemService.getItemById(orderParamsDTO.getItemId());

        order.setCreatedTime(DateUtils.getDate());
        order.setOrderType(0);
        order.setOrderNo(orderNo);
        order.setItemName(item.getItemName());
        order.setInvoiceNo(orderParamsDTO.getInvoiceId());
        if (orderParamsDTO.getIsNeedInsurance() != null && orderParamsDTO.getIsNeedInsurance() != 0) {
            order.setInsuranceAmount(Constant.INSURANCE_AMOUNT);
        }
        //解析数据 拿到商品总数和商品总价
        String [] seatArray = orderParamsDTO.getSeatPositions().split(",");
        //通过排期和座位查询商品的价格
        List<DmSeatPriceOrderDTO> seatPriceOrderDTOList = schedulerSeatPriceService.getSeatPriceOrderInfo(orderParamsDTO.getSchedulerId(),seatArray);
        order.setTotalCount(seatPriceOrderDTOList.size());
        double totalAmount = seatPriceOrderDTOList.stream().mapToDouble(DmSeatPriceOrderDTO::getPrice).sum();
        order.setTotalAmount(totalAmount);
        orderMapper.insert(order);

        /*准备订单的关联用户数据*/

        List<DmOrderLinkUser> orderLinkUserList = new ArrayList<>();
        String [] linkUserIds = orderParamsDTO.getLinkIds().split(",");
        List<DmLinkUser> linkUserList = linkUserService.getLinksUsersByIdArray(linkUserIds);
        for (int i = 0; i < linkUserList.size(); i++) {
            DmLinkUser linkUser = linkUserList.get(i);
            DmSeatPriceOrderDTO seatPriceOrderDTO = seatPriceOrderDTOList.get(i);
            DmOrderLinkUser orderLinkUser = new DmOrderLinkUser();
            orderLinkUser.setLinkUserId(linkUser.getId());
            orderLinkUser.setLinkUserName(linkUser.getName());
            orderLinkUser.setX(seatPriceOrderDTO.getX());
            orderLinkUser.setY(seatPriceOrderDTO.getY());
            orderLinkUser.setPrice(seatPriceOrderDTO.getPrice());
            orderLinkUser.setOrderId(order.getId());
            orderLinkUser.setCreatedTime(new Date());
            orderLinkUserList.add(orderLinkUser);
        }

        orderLinkUserService.addLinkUsers(orderLinkUserList);

        List<DmSchedulerSeat> schedulerSeatList = new ArrayList<>();
        for (DmSeatPriceOrderDTO seatPriceOrderDTO : seatPriceOrderDTOList) {
            DmSchedulerSeat schedulerSeat = new DmSchedulerSeat();
            schedulerSeat.setOrderNo(order.getOrderNo());
            schedulerSeat.setUserId(order.getUserId());
            schedulerSeat.setStatus(2);
            schedulerSeat.setId(seatPriceOrderDTO.getScheduleSeatId());
            schedulerSeatList.add(schedulerSeat);
        }
        /*到这里 真正的全部做完*/

        for (DmSchedulerSeat dmSchedulerSeat : schedulerSeatList) {
            schedulerSeatService.updateSeatByOrder(dmSchedulerSeat);
        }
        return order;
    }
}
